package com.jlastu.dlxy_androidapp.view.details;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.DetailsOrderAdapter;
import com.jlastu.dlxy_androidapp.entity.OrderCart;

import java.util.ArrayList;

public class Cart extends AppCompatActivity implements DetailsOrderAdapter.OnTotalAmountChangeListener,DetailsOrderAdapter.OnAllCheckboxStateChangeListener {
    private TextView totalTextView;
    CheckBox allCheckbox;
    String type;
    ArrayList<OrderCart> ordercartsList = null;
    Button checkout_button;
    ListView listView;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart_1);
        Intent intent = getIntent();
        if (intent != null) {
            ordercartsList = (ArrayList<OrderCart>) intent.getSerializableExtra("ordercartsList");
            type=getIntent().getStringExtra("type");
        }
        listView = findViewById(R.id.cart_listview);
        DetailsOrderAdapter orderAdapter = new DetailsOrderAdapter(this,ordercartsList,this);
        orderAdapter.setOnAllCheckboxStateChangeListener(this);
        listView.setAdapter(orderAdapter);
        //长按事件
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // 创建PopupMenu
                PopupMenu popupMenu = new PopupMenu(Cart.this, view);

                // 添加单个菜单项 "删除信息"
                popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, "删除商品"); // 使用ID 1标识删除菜单项
                // 设置菜单项点击事件
                popupMenu.setOnMenuItemClickListener(item -> {
                    switch (item.getItemId()) {
                        case 1:
                            // 执行删除操作
                            deleteOrder(position);
                            // 更新界面
                            ((DetailsOrderAdapter) listView.getAdapter()).notifyDataSetChanged();
                            return true;
                    }
                    return false;
                });

                // 显示PopupMenu
                popupMenu.show();
                return true; // 返回true，表示事件已消费
            }
        });

        totalTextView = findViewById(R.id.total_price_text);

        //全选按钮
        allCheckbox = findViewById(R.id.all); // 全选按钮
        // 设置全选按钮的监听器
        allCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 更新所有订单项的选中状态
                orderAdapter.setAllSelected(isChecked);
            }
        });
        //返回按钮
        ImageView backButton = findViewById(R.id.back_button);
        // 设置返回按钮的点击事件
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        checkout_button=findViewById(R.id.checkout_button);
        checkout_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<OrderCart> selectedOrderCarts = new ArrayList<>();
                for (OrderCart cart :ordercartsList) {
                    if (cart.isSelected()) {
                        cart.setSelected(false);
                        selectedOrderCarts.add(cart);
                    }
                }
                if(selectedOrderCarts.size()==0){
                    Toast.makeText(getApplicationContext(), "还没有选择商品哦", Toast.LENGTH_SHORT).show();
                }else{
                    // 传递新列表到OrderActivity
                    Intent intent = new Intent(Cart.this, OrderActivity.class);
                    intent.putExtra("ordercartsList", selectedOrderCarts);
                    startActivity(intent);
                }


            }
        });


    }
    @Override
    public void onTotalAmountChanged(double totalAmount) {
        // 更新总价TextView
        totalTextView.setText(String.format("总价：¥%.2f", totalAmount));
    }
    @Override
    public void onAllCheckboxStateChanged() {
        // 当 Adapter 调用此方法时，更新全选按钮的状态
        updateAllCheckboxState();
    }
    private void updateAllCheckboxState() {
        boolean allSelected = true;
        for (OrderCart order : ordercartsList) {
            if (!order.isSelected()) {
                allSelected = false;
                break;
            }
        }
        allCheckbox.setChecked(allSelected);
    }
    private void deleteOrder(int position) {
        // 从列表中删除订单
        ordercartsList.remove(position);
        // 通知数据集改变
        ((DetailsOrderAdapter) listView.getAdapter()).notifyDataSetChanged();
    }
}